#ifndef JSVM_CONVERTER_H
#define JSVM_CONVERTER_H

#include "ark_runtime/jsvm.h"
#include "common.h"
#include "JSVMRuntime.h"

namespace rnjsvm {
    class JSVMConverter {
    public:
        static Value JSVMToJsi(JSVM_Env env, const JSVM_Value value);
        static JSVM_Value JsiToJSVM(JSVM_Env env, const Value &value);

        template<typename T>
  		static Value JsiValueFromPointerValue(JSVM_Env env, const JSVM_Value value) {
    		return JSVMRuntime::make<T>(new JSVMPointerValue(env, value));
  		}

        static Value JsiValueFromNumber(JSVM_Env env, const JSVM_Value value) {
            double result;
            OH_JSVM_GetValueDouble(env, value, &result);
            return Value(result);
        }

        static Value JsiValueFromBool(JSVM_Env env, const JSVM_Value value) {
            bool result;
            OH_JSVM_GetValueBool(env, value, &result);
            return Value(result);
        }

        template<typename T>
  		static JSVM_Value PointerValueToJSVM(JSVM_Env env, const T &pointer) {
    		return static_cast<const JSVMPointerValue *>(JSVMRuntime::getPointerValue(pointer))->GetValue();
  		}

  		template<typename T>
  		static T make(JSVM_Env env, JSVM_Value value) {
    		if (!value) {
                OH_JSVM_GetUndefined(env, &value);
    		}
    		return JSVMRuntime::make<T>(new JSVMPointerValue(env, value));
  		}
	};
}
#endif